import Vditor from "vditor";
import {ElLoading, ElNotification} from "element-plus";
import emitter from "@/common/util/mitt";


export var initMarkDown = (options, id, input, node = null) => {
    try {
        var contentEditor = null;
        let config = Object.assign({}, options)
        // config.cdn = window.getScriptPath() + '/resource/'
        config.upload = {
            handler(file) {
                for (let i in file) {
                    contentEditor.insertValue("![](file://" + file[i].path + ")\n")
                }
            }
        }

        config.input = input;
        config.after = () => {
            contentEditor.disabled()
            if (node) {
                updataDoc(node, "#markdown", contentEditor)
            }
        }
        contentEditor = new Vditor(id, config);
    }catch (e) {
        ElNotification({
            title: '错误',
            message: 'MarkDown加载失败！\n' + e,
            type: 'error',
        })
    }
    return contentEditor;
}

let renderReadMode = (readMode, data, vditor) => {
    let options = Object.assign({}, readMode.options)
    // let config =  readMode.options
    options.after = () => {
        Vditor.outlineRender(readMode.element, readMode.outline);
        // Vditor.setCodeTheme(config.preview.hljs.style)
        // Vditor.setContentTheme(config.preview.theme)
        vditor.setTheme(options.theme, options.preview.theme.current, options.preview.hljs.style);

        if (readMode.outline.innerText.trim() !== '') {
            readMode.outline.style.display = 'block'
        }
    }
    Vditor.preview(readMode.element, data, options)
    renderOutline(vditor, "markdown-outline")
}
export var updataDoc = (node, loadingElement, vditor,
                        readMode = {enable: false, element: "", options: {}}) => {
    if (!node) {
        return
    }

    let loadingInstance = ElLoading.service({
        lock: true,
        text: '文件较大，加载中。。。',
        spinner: 'el-icon-loading',
        target: loadingElement
    })

    window.readFile(node.path, (err, data) => {
        if (err) {
            ElNotification({
                title: '错误',
                message: '文件读取失败！\n' + err,
                type: 'error',
            })
            loadingInstance.close();
        } else {
            if (readMode.enable) {
                renderReadMode(readMode, data, vditor)
            } else {
                vditor.setValue(data)
                vditor.enable()
            }
            loadingInstance.close();
        }
    });
}

export var renderOutline = (vditor, id) => {
    vditor.vditor.outline.render(vditor.vditor, document.getElementById(id))
}

export var saveDoc = (queue) => {
    let path = queue.path
    let value = queue.content

    if (!path || value == "") {
        queue.isWrite = true
        return
    }

    window.reWrite(path, value, (err, code) => {
        if (err) {
            if (code == window.FILE_NOT_FOUND) {
                console.error("文件不存在", path)
                queue.isWrite = true
            } else {
                console.log(err)
                queue.isWrite = false
            }
        } else {
            console.debug("写入文件成功", path)
            queue.isWrite = true
        }
    })
}

